import base from "@src/store/base";
import mall from "@src/store/mall";
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import {getGroupDetails} from "@src/api/group"
import router from "@src/router"
import {fanyCountdown} from "../../utils/util"
Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: mall,
    fields: ['userLocation'],
    actions: ['initLocation', 'setAddress']
  },{
    store: base,
    fields: ['isiPhoneX', 'themeColor', 'themeTitleColor'],
  }],
  data: {
    //是否在定位中
    isLocation: false,
    groupInfo:null,//拼团相关信息
    groupNo:null,
    timeInterval:null,
    shareGroupImg:null,
    items:null
  },

  methods: {
    onLoad: function (options) {
      this.setData({groupNo:options.groupNo})
      wx.showShareMenu({
        withShareTicket: true,
        menus: ['shareAppMessage', 'shareTimeline']
      })
    },
    // 获取拼团海报
    groupPainter(e){
      console.log('海报',e.detail)
      // this.data.shareGroupImg=e.detail;
      this.setData({shareGroupImg:e.detail})
    },
    onShareAppMessage: function (res) {
      if (res.from === 'button') {
        // 来自页面内转发按钮
        console.log(res.target)
      }
      return {
        title: this.data.items.title,
        path: `sp-mall/pages/join-group/index?groupNo=${this.data.groupInfo.groupNo}`,
        imageUrl: this.data.shareGroupImg||getApp().toFullUrl(this.data.items.coverImg),
      }
    },
    //初始化拼团相关数据
    initData(data){
      let params = {groupNo:data}
      getGroupDetails(params).then(res=>{
        //处理拼团中图像地址
        if(res.data){
          let fileBaseUrl = getApp().globalData.extConfig.fileBaseUrl;
          res.data.userList.forEach((v,i)=>{
            if(!v.headImg.includes('http'))  v.headImg=fileBaseUrl+'/'+v.headImg
          })
        }
        this.setData({groupInfo:res.data})
        let items = {};
        items.price=res.data.price;
        items.originPrice=res.data.originPrice;
        items.coverImg=res.data.goodsCover;
        items.title = res.data.goodsTitle;
        this.setData({items})
        if(res.data) {
          const wxObj=this;
          fanyCountdown(res.data.expireTime,wxObj)
        }
      })
    },
    onShow(){
      if(this.data.groupInfo) {
        const wxObj=this;
        fanyCountdown(this.data.groupInfo.expireTime,wxObj)
      }
    },
    onLogin(){
      this.initData(this.data.groupNo);
    },
    onHide(){
      clearInterval(this.data.timeInterval);
    },
    relocationFunc() {
      const that = this;
      this.setData({ isLocation: true })
      this.initLocation(true).then((res) => {
        setTimeout(() => {
          that.setData({ isLocation: false })
        }, 1000)
      })
    },
    //点击立即参团按钮进入到商品详情
    joinGroup(){
      const params = {
        goodsSerial:this.data.groupInfo.goodsSerial,
        storeId:this.data.groupInfo.storeId,
        groupNo:this.data.groupInfo.groupNo,
        groupActivityNo:this.data.groupInfo.activityNo,
        creatorDeliveryType:this.data.groupInfo.creatorDeliveryType
      }
      router.push({
        name:"mall/goods-detail",
        params,
        success: routerRes => {
          routerRes.eventChannel.emit('joinGroupToGoodDetails', this.data.groupInfo);
        }
      })
    },
    toGoodsDetails(){
      const params={
        storeId:this.data.groupInfo.storeId,
        goodsSerial:this.data.groupInfo.goodsSerial,
        creatorDeliveryType:this.data.groupInfo.creatorDeliveryType
      }
      router.push({name:"mall/goods-detail",params})
    },
    toSpellGroupPer:function (){
      //leaderCollect: 是否代收字段
      router.push({name:"mall/spellGroupPer",params:{orderNo:this.data.groupInfo.creatorOrderNo,status:this.data.groupInfo.status}})
    },

    onPullDownRefresh(){
        this.initData(this.data.groupNo);
      clearInterval(this.data.timeInterval);
      setTimeout(() => {
        this.setData({loading: false})
        wx.stopPullDownRefresh();
      }, 300)
    }
  }
})
